<?php
/**
 * CreateTime: 2022/7/6 17:18
 */

namespace App\Service\Approval\ApprovalCreditClient;


use App\Models\Client\ClientModel;
use App\Models\Client\CreditClientModel;
use App\Service\Approval\DingTalkApprovalInterFace;
use App\Tools\Log\DingTalkLog;
use App\Tools\Log\Log;
use Illuminate\Support\Facades\DB;

class ApprovalCreditClient implements DingTalkApprovalInterFace
{

    public function approvalStart($processInstanceId,$sourceId,$processStatus)
    {
        # 信用客户审批开始
        $clientModel = new ClientModel();
        Log::e("修改");
        try {
            // 如果当前客户的ID不为8 则不允许修改
            $client_status = $clientModel->getClientInfoByClientId($sourceId,['client_status'])['client_status'];
            if($client_status==8){
                // 修改客户信用客户送审状态为送审中
                $clientModel->editClient([
                    'client_id' => $sourceId,
                    'client_status' => 3
                ]);
            }
            DB::commit();
        }catch (\Exception $exception){
            DingTalkLog::errDingTalkLog($exception,$processInstanceId);
            DB::rollBack();
        }
    }


    public function approvalAgreeEnd($processInstanceId, $sourceId,$processStatus)
    {
        Log::e("正常结束");
        $clientModel = new ClientModel();
        $creditClientModel = new CreditClientModel();
        // 如果当前客户的ID不为8 则不允许修改
        $client_status = $clientModel->getClientInfoByClientId($sourceId,['client_status'])['client_status'];
        if($client_status!=3){
            // 修改客户信用客户送审状态为送审中
            return [];
        }
        DB::beginTransaction();
        try {
            # 修改客户表是否信用客户 is_credit_client 客户类型 client_type
            $clientModel->editClient([
                'client_id' => $sourceId,
                'is_credit_client' => 1,
                'client_type' => 2,
                'client_status' => 1
            ]);
            # 获取最新的信用客户表ID 把其他的状态变更为历史
            $creditId = $creditClientModel->getCreditClientListByClientId($sourceId,['credit_client_id'])['credit_client_id'];
            $creditClientModel->where('credit_client_id','!=',$creditId)->where('client_id',$sourceId)->update([
                'credit_client_status' => 2
            ]);
            DB::commit();
        }catch (\Exception $exception){
            DingTalkLog::errDingTalkLog($exception,$processInstanceId);
            DB::rollBack();
        }
    }

    public function approvalRefuseEnd($processInstanceId,$sourceId,$processStatus){
        Log::e("异常结束");
        $clientModel = new ClientModel();
        $client_status = $clientModel->getClientInfoByClientId($sourceId,['client_status'])['client_status'];
        if($client_status!=3){
            // 修改客户信用客户送审状态为送审中
            return [];
        }
        DB::beginTransaction();
        try {
            # 修改客户表是否信用客户 is_credit_client 客户类型 client_type
            $clientModel->editClient([
                'client_id' => $sourceId,
                'client_status' => 1
            ]);
            DB::commit();
        }catch (\Exception $exception){
            Log::e($exception->getMessage());
            DingTalkLog::errDingTalkLog($exception,$processInstanceId);
            DB::rollBack();
        }
    }
}